package in.muton.edushare.components.tasks.invitations;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.HashMap;
import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CourseDeptLinkDAO;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.dao.EnrolmentsDAO;
import com.edushare.common.dao.UserDeptLinkDAO;
import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class ThirdPartyCourseInvite extends EdusharePanel {
	private static final long serialVersionUID = 7253070503620996461L;
	private GenericRenderer<Integer> gr;
	private IModel<CourseInstances> cInstModel = null;
	private IModel<Users> owner;

	public ThirdPartyCourseInvite(String id, IModel<Users> model,
			EduShareBasePage page) {
		super(id, model, page);
		this.owner = model;
		add(CSSPackageResource.getHeaderContribution(
				ThirdPartyCourseInvite.class, "Invitations.css"));
		Form<Void> f = new Form<Void>("dummyForm");
		add(f);
		f.add(new Label("userName", owner.getObject().getCompleteUserName()));
		f.add(new Button("studentInvite") {
			@Override
			public void onSubmit() {
				EnrolmentsDAO eDAO = new EnrolmentsDAO(getORMInterface());
				getSelectedInst();
				setError(eDAO.addUserFB(cInstModel.getObject(), owner
						.getObject(), Enrolments.STUDENT, EduShareSession.get()
						.getLoggedInUser()));
			}

			private static final long serialVersionUID = 5089484037645395257L;

		});
		f.add(new Button("facInvite") {
			@Override
			public void onSubmit() {
				EnrolmentsDAO eDAO = new EnrolmentsDAO(getORMInterface());
				getSelectedInst();
				setError(eDAO.addUserFB(cInstModel.getObject(), owner
						.getObject(), Enrolments.TEACHER, EduShareSession.get()
						.getLoggedInUser()));
			}

			private static final long serialVersionUID = -172831209438806526L;

		});
		f.add(new Button("obsInvite") {
			@Override
			public void onSubmit() {
				EnrolmentsDAO eDAO = new EnrolmentsDAO(getORMInterface());
				getSelectedInst();
				setError(eDAO.addUserFB(cInstModel.getObject(), owner
						.getObject(), Enrolments.OBSERVER, EduShareSession
						.get().getLoggedInUser()));
			}

			private static final long serialVersionUID = -11916945719192290L;

		});
		f.add(new Button("TAInvite") {
			@Override
			public void onSubmit() {
				EnrolmentsDAO eDAO = new EnrolmentsDAO(getORMInterface());
				getSelectedInst();
				setError(eDAO.addUserFB(cInstModel.getObject(), owner
						.getObject(), Enrolments.TEACHING_ASSISTANT,
						EduShareSession.get().getLoggedInUser()));
			}

			private static final long serialVersionUID = 1995297257993229621L;
		});

		gr = new GenericRenderer<Integer>(getTaughtCourses(), null);
		f.add(new DropDownChoice<String>("courseSelect", gr.getModel(), gr
				.getOptions(), gr));
	}

	private void getSelectedInst() {
		if (gr.getSelection() == null) {
			setError("Please choose a course to invite "
					+ owner.getObject().getCompleteUserName() + "to");
			return;
		}
		int instId = gr.getSelection();
		CourseInstances cInst = new CourseInstancesDAO(getORMInterface())
				.getCourseInstanceById(instId);
		cInstModel = new CayenneModel<CourseInstances>(cInst);
	}

	private HashMap<String, Integer> getTaughtCourses() {
		Users user = getLoggedInUser();
		EnrolmentsDAO eDAO = new EnrolmentsDAO(getORMInterface());
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		UserRolesDAO uDAO = new UserRolesDAO(getORMInterface());
		if (uDAO.isUserAdmin(getLoggedInUser())) {
			Universities univ = uDAO.getAdministeredUniv(getLoggedInUser());
			List<Courses> courseList = new CoursesDAO(getORMInterface())
					.getUnivCourses(univ);
			for (Courses course : courseList) {
				CourseInstances cInst = new CourseInstancesDAO(
						getORMInterface()).getActiveInst(course);
				if (cInst != null) {
					boolean isPresent = eDAO.isUserPresent(cInst, owner
							.getObject());
					if (!isPresent) {
						map.put(cInst.getCourse().getCourseName(), cInst
								.getCourseInstanceId());
					}
				}
			}
		}

		UserDeptLinkDAO udDAO = new UserDeptLinkDAO(getORMInterface());
		List<Departments> deptList = udDAO.getDeptByUserRole(getLoggedInUser(),
				UserDeptLink.PRIMARY_ADMIN);
		for (Departments d : deptList) {
			List<Courses> courseList = new CourseDeptLinkDAO(getORMInterface())
					.getAllCourses(d);
			for (Courses course : courseList) {
				CourseInstances cInst = new CourseInstancesDAO(
						getORMInterface()).getActiveInst(course);
				if (cInst != null) {
					boolean isPresent = eDAO.isUserPresent(cInst, owner
							.getObject());
					if (!isPresent
							&& map.get(cInst.getCourse().getCourseName()) != null) {
						map.put(cInst.getCourse().getCourseName(), cInst
								.getCourseInstanceId());
					}
				}
			}
		}

		List<Enrolments> enrolList = user.getEnrolments();
		for (Enrolments e : enrolList) {
			if (e.getEnrolmentType() == Enrolments.TEACHER
					&& e.getCourseInstance().isActive()) {
				CourseInstances c = e.getCourseInstance();
				boolean isPresent = eDAO.isUserPresent(c, owner.getObject());
				if (!isPresent
						&& map.get(c.getCourse().getCourseName()) != null)
					map.put(c.getCourse().getCourseName(), c
							.getCourseInstanceId());
			}
		}
		return map;
	}

	@Override
	public boolean isVisible() {
		return getTaughtCourses().size() > 0
				&& getLoggedInUser() != owner.getObject();
	}
}
